package com.jinzecloud.reportapproval.service.impl;


import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jinzecloud.reportapproval.bean.ConsistencyCheck;
import com.jinzecloud.reportapproval.mapper.ConsistencyCheckMapper;
import com.jinzecloud.reportapproval.service.ConsistencyCheckService;
import com.jinzecloud.reportapproval.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.servlet.http.HttpServletRequest;


@Service("consistencyCheckService")
public class ConsistencyCheckServiceImpl extends ServiceImpl<ConsistencyCheckMapper, ConsistencyCheck> implements ConsistencyCheckService {

    @Autowired
    private FileUploadUtil fileUploadUtil;
    @Autowired
    private HttpServletRequest request;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ConsistencyCheck> page = this.page(
                new Query<ConsistencyCheck>().getPage(params),
                new QueryWrapper<ConsistencyCheck>().like("test_name",params.get("key"))
        );

        return new PageUtils(page);
    }

    @Override
    public String createPdf(Integer id) {
        ConsistencyCheck consistencyCheck=getById(id);
        JSONObject data = JSONObject.parseObject(JSONObject.toJSONString(consistencyCheck));
        List<Map<String, String>> jsonData = new ArrayList<>();
        // 获取 check_result JSONArray
        JSONArray checkResultArray = data.getJSONArray("checkResult");
        // 默认值初始化
        data.put("bzresult1", "□ 一致");
        data.put("bzresult2", "□ 不一致");
        data.put("bzresult3", "□ 不适用");
        data.put("smresult1", "□ 一致");
        data.put("smresult2", "□ 不一致");
        for (int i = 0; i < checkResultArray.size(); i++) {
            JSONObject jsondata = checkResultArray.getJSONObject(i);
            // 从 jsondata 中获取 result 和 consistency
            String result = jsondata.getString("result");
            String consistency = jsondata.getString("consistency");
            int row = jsondata.getInteger("row");
            // 根据 row 字段处理结果
            if (row == 1) {
                // 第一行
                data.put("bzresult1", "一致".equals(result) ? "■ 一致" : "□ 一致");
                data.put("bzresult2", "不一致".equals(result) ? "■ 不一致" : "□ 不一致");
                data.put("bzresult3", "不适用".equals(result) ? "■ 不适用" : "□ 不适用");
                data.put("bzpd", consistency);
            } else if (row == 2) {
                // 第二行
                data.put("smresult1", "一致".equals(result) ? "■ 一致" : "□ 一致");
                data.put("smresult2", "不一致".equals(result) ? "■ 不一致" : "□ 不一致");
                data.put("smpd", consistency);
            }

            data.put("smpd", consistency);
        }
        File tempFile = WordUtil.getWordFile("template/consistency.docx", data);

        if (tempFile == null) return null;
        String fileName = "产品规格表与产品技术文件一致性检查表";
        String uploadDirectory = fileUploadUtil.getUploadFilePath("temp");
        String filePath = uploadDirectory + consistencyCheck.getProjectNum() + fileName;
        String savePath = RequestUtils.getBasePath(request) + "upload/temp/" + consistencyCheck.getProjectNum() +fileName;
        FileUtil.copy(tempFile, new File(filePath + ".docx"),true);
        PDFUtil.WordToPdf(tempFile, new File(filePath + ".pdf"));

        update(new LambdaUpdateWrapper<ConsistencyCheck>().set(ConsistencyCheck::getWordPath, savePath + ".docx").set(ConsistencyCheck::getPdfPath, savePath + ".pdf").eq(ConsistencyCheck::getId, id));
        return RequestUtils.getBasePath(request) + "upload/temp/" + consistencyCheck.getProjectNum() + fileName + ".pdf" ;
    }

    @Override
    public JSONObject consistencyCheckJson(String projectUuid) {
        ConsistencyCheck consistencyCheck=getOne(new QueryWrapper<ConsistencyCheck>().eq("project_num", projectUuid));
        if (consistencyCheck == null) return null;
        JSONObject data = JSONObject.parseObject(JSONObject.toJSONString(consistencyCheck));
        List<Map<String, String>> jsonData = new ArrayList<>();
        // 获取 check_result JSONArray
        JSONArray checkResultArray = data.getJSONArray("checkResult");
        // 默认值初始化
        data.put("bzresult1", "□ 一致");
        data.put("bzresult2", "□ 不一致");
        data.put("bzresult3", "□ 不适用");
        data.put("smresult1", "□ 一致");
        data.put("smresult2", "□ 不一致");
        for (int i = 0; i < checkResultArray.size(); i++) {
            JSONObject jsondata = checkResultArray.getJSONObject(i);
            // 从 jsondata 中获取 result 和 consistency
            String result = jsondata.getString("result");
            String consistency = jsondata.getString("consistency");
            int row = jsondata.getInteger("row");
            // 根据 row 字段处理结果
            if (row == 1) {
                // 第一行
                data.put("bzresult1", "一致".equals(result) ? "☑ 一致" : "□ 一致");
                data.put("bzresult2", "不一致".equals(result) ? "☑ 不一致" : "□ 不一致");
                data.put("bzresult3", "不适用".equals(result) ? "☑ 不适用" : "□ 不适用");
                data.put("bzpd", consistency);
            } else if (row == 2) {
                // 第二行
                data.put("smresult1", "一致".equals(result) ? "☑ 一致" : "□ 一致");
                data.put("smresult2", "不一致".equals(result) ? "☑ 不一致" : "□ 不一致");
                data.put("smpd", consistency);
            }

            data.put("smpd", consistency);
        }
        return data;
    }

    @Override
    public String createWord(Integer id) {
        ConsistencyCheck consistencyCheck=getById(id);
        JSONObject data = JSONObject.parseObject(JSONObject.toJSONString(consistencyCheck));
        List<Map<String, String>> jsonData = new ArrayList<>();
        // 获取 check_result JSONArray
        JSONArray checkResultArray = data.getJSONArray("checkResult");
        // 默认值初始化
        data.put("bzresult1", "□ 一致");
        data.put("bzresult2", "□ 不一致");
        data.put("bzresult3", "□ 不适用");
        data.put("smresult1", "□ 一致");
        data.put("smresult2", "□ 不一致");
        for (int i = 0; i < checkResultArray.size(); i++) {
            JSONObject jsondata = checkResultArray.getJSONObject(i);
            // 从 jsondata 中获取 result 和 consistency
            String result = jsondata.getString("result");
            String consistency = jsondata.getString("consistency");
            int row = jsondata.getInteger("row");
            // 根据 row 字段处理结果
            if (row == 1) {
                // 第一行
                data.put("bzresult1", "一致".equals(result) ? "■ 一致" : "□ 一致");
                data.put("bzresult2", "不一致".equals(result) ? "■ 不一致" : "□ 不一致");
                data.put("bzresult3", "不适用".equals(result) ? "■ 不适用" : "□ 不适用");
                data.put("bzpd", consistency);
            } else if (row == 2) {
                // 第二行
                data.put("smresult1", "一致".equals(result) ? "■ 一致" : "□ 一致");
                data.put("smresult2", "不一致".equals(result) ? "■ 不一致" : "□ 不一致");
                data.put("smpd", consistency);
            }

            data.put("smpd", consistency);
        }
        File tempFile = WordUtil.getWordFile("template/consistency.docx", data);

        if (tempFile == null) return null;
        String fileName = "产品规格表与产品技术文件一致性检查表";
        String uploadDirectory = fileUploadUtil.getUploadFilePath("temp");
        String filePath = uploadDirectory + consistencyCheck.getProjectNum() + fileName;
        String savePath = RequestUtils.getBasePath(request) + "upload/temp/" + consistencyCheck.getProjectNum() +fileName;
        FileUtil.copy(tempFile, new File(filePath + ".docx"),true);
        PDFUtil.WordToPdf(tempFile, new File(filePath + ".pdf"));

        update(new LambdaUpdateWrapper<ConsistencyCheck>().set(ConsistencyCheck::getWordPath, savePath + ".docx").set(ConsistencyCheck::getPdfPath, savePath + ".pdf").eq(ConsistencyCheck::getId, id));
        return RequestUtils.getBasePath(request) + "upload/temp/" + consistencyCheck.getProjectNum() + fileName + ".docx" ;
    }

}